package net.mikoo.seals.biz.manager;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import javax.activation.MimetypesFileTypeMap;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;

@Component
public class OssManager {
	
	
	@Value("${aliyun.oss.endpoint}")
	private  String endpoint;
	
	@Value("${aliyun.oss.accessKeyId}")
	private  String accessKeyId;
	
	@Value("${aliyun.oss.accessKeySecret}")
	private  String accessKeySecret;
	
	@Value("${aliyun.oss.bucketName}")
	private  String bucketName;
	
	
	
	public OSSClient getOssClient(){
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		return ossClient;
	}
	
	
	public String getBucketName(){
		
		return bucketName;
	}
	
	public void close(OSSClient ossClient){
		ossClient.shutdown();
	}
	
	public  void upload(String filename,InputStream file){
		
		ClientConfiguration conf = new ClientConfiguration();
		conf.setSupportCname(true);
		
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		ossClient.putObject(bucketName, filename, file);
		ossClient.shutdown();
	}
	
	public  void upload(String filename,String content){
		
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 创建上传Object的Metadata
		ObjectMetadata meta = new ObjectMetadata();
		meta.setContentType("application/octet-stream");
		ossClient.putObject(bucketName, filename, new ByteArrayInputStream(content.getBytes()), meta);
		// 关闭client
		ossClient.shutdown();
	}

	
	public static void main(String[] args) {
		File f  =new File("/Users/huangxiaoming/Downloads/qixi1/js/main2.js");
		System.out.println("Mime Type of " + f.getName() + " is " +  
                new MimetypesFileTypeMap().getContentType(f));  
	}
	
	
}
